package cn.xzc.job.type.cron;

import cn.xzc.job.type.TriggerJob;

import java.util.Objects;

/**
 * cron表达式实现的定时任务
 *
 * @author xzc
 * 当前时间 2024-02-12 21:30:00
 */
public class CronJob extends TriggerJob {

    private final String expression;


    /**
     * Create a new {@code CronJob}.
     *
     * @param runnable   the underlying task to execute
     * @param expression the cron expression defining when the task should be executed
     */
    public CronJob(Runnable runnable, String expression) {
        this(runnable, new CronTrigger(expression));
    }

    /**
     * Create a new {@code CronJob}.
     *
     * @param runnable    the underlying task to execute
     * @param cronTrigger the cron trigger defining when the task should be executed
     */
    public CronJob(Runnable runnable, CronTrigger cronTrigger) {
        super(runnable, cronTrigger);
        this.expression = cronTrigger.getExpression();
    }


    /**
     * Return the cron expression defining when the task should be executed.
     */
    public String getExpression() {
        return this.expression;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof CronJob)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        CronJob cronJob = (CronJob) o;
        return Objects.equals(expression, cronJob.expression);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), expression);
    }
}